require(["config"], function () {
    require(["jquery", "template", "cookie", "load"], function ($, template) {
        function Cart() {

            this.cart = [];
            $.cookie.json = true;
            this.render();
            this.addListener();

        }
        $.extend(Cart.prototype, {
            render() {
                const cart = this.cart = $.cookie("cart") || [];
                if (cart.length === 0) {
                    $(".cart_nav").removeClass("hidden").next(".cart_gray_box").addClass("hidden");
                    return;
                } else {
                    $(".cart_nav").addClass("hidden").next(".cart_gray_box").removeClass("hidden");
                }

                const html = template("cart-template", {
                    "cart": cart
                });
                $(".cart_body").html(html);
                // $(".sum_price").text("￥ " + ((cart[0].price.slice(2)*cart[0].amount).toFixed(2)));
            },
            addListener() {
                //删除
                $(".cart_gray_box").on("click", ".link-del", $.proxy(this.delHandler, this));
                //加减
                $(".cart_body").on("click", ".decrement,.increment", $.proxy(this.jiaJian, this));
                //输入修改数量
                $(".cart_body").on("blur", ".amount", $.proxy(this.jiaJian, this));
                //全选
                $(".chk_all").on("click", this.chkAllHandler);
                //部分选中
                $(".cart_body").on("click", ".chk_prod", $.proxy(this.chkProdHandler, this));
                //事件
                $(".cart_gray_box").on("click", ".chk_all,.chk_prod,.increment,.decrement,.link-del",this.calcTotalHandler);
            },
            delHandler(event) {
                const $tr = $(event.target).parents("tr");
                const id = $tr.find(".id").text();
                this.cart = this.cart.filter(curr => curr.id != id);
                $.cookie("cart", this.cart, {
                    expires: 10,
                    path: "/"
                });
                // 页面DOM树中删除行
                $tr.remove();
                this.render();
            },
            jiaJian(event) {
                const
                    $src = $(event.target),
                    $tr = $src.parents("tr");
                const id = $tr.find(".id").text();
                const prod = this.cart.filter(curr => curr.id == id)[0];
                //console.log(prod)

                //修改数量
                if ($src.is(".increment")) //加
                    prod.amount++;
                else if ($src.is(".decrement")) {
                    if (prod.amount <= 1)
                        return;
                    prod.amount--;
                } else if($src.is(".amount")){
                    const _amount = $src.val();
                    //判断是否符合数字
                    const reg = /^[1-9]\d*$/;
                    if(!reg.test(_amount)){
                        $src.val(prod.amount);
                        return;
                    }
                    prod.amount = Number(_amount);
                }

                //保存cookie
                $.cookie("cart", this.cart, {
                    expires: 10,
                    path: "/"
                });
                $tr.find(".amount").val(prod.amount);
                $tr.find(".sub").text((prod.price * prod.amount).toFixed(2));
            },
            chkAllHandler(event){
                // 获取当前“全选”复选框选中状态
				// checked、selected、disabled 通常使用 .prop() 方法获取/设置
                const status =  $(event.target).prop("checked")
                //让每行的复选框与全选状态保持一致
                $(".chk_prod").prop("checked", status);
            },
            //部分选中
            chkProdHandler(){
                const count = $(".chk_prod:checked")
                $(".chk_all").prop("checked", count === this.cart.length);
            },
            //计算合计金额
            calcTotalHandler(){
                let sum = 0;
                $(".chk_prod:checked").each((index, element)=>{
                    sum += Number($(element).parents("tr").find(".sub").text())
                });
                $(".total").text(sum.toFixed(2));
            }
        });
        new Cart();
    });
});